package textapp.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import textapp.business.GroupMessageService;
import textapp.business.MessageService;
import textapp.connector.MessageCode;
import textapp.connector.ServerConnectorMessage;
import textapp.dao.model.GroupMessage;
import textapp.dao.model.Message;

/**
 * Servlet implementation class SaveGroupMessageJsonServlet
 */
public class SaveGroupMessageJsonServlet extends GenericJsonServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveGroupMessageJsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	protected Object executeRequestJson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try {
			int to = Integer.parseInt(request.getParameter("togroup"));
			int from = Integer.parseInt(request.getParameter("from"));
			int type = Integer.parseInt(request.getParameter("type"));
			String text = request.getParameter("text");
			int lifetime = Integer.parseInt(request.getParameter("lifetime"));
			
			GroupMessage groupMsg = new GroupMessageService().saveGroupMessage(text, to, from, type, lifetime);
			return new ServerConnectorMessage(MessageCode.SUCCESS, groupMsg);
			
			
		} catch(NumberFormatException ex) {
			return new ServerConnectorMessage(MessageCode.FAIL);
		}
		
	}

	

}
